package com.greenant.store.broadcast;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogUtil;
import com.greenant.store.R;
import com.greenant.store.activity.MyMsgActivity;
import com.greenant.store.utils.SQLiteHelper;
import com.greenant.store.utils.iLog;

import org.json.JSONException;
import org.json.JSONObject;

public class MallManagerReceiver extends BroadcastReceiver
{

    private static final String TAG = "DownloadManagerReceiver";
    public static final String ACTION = "com.greenantmall.MallManagerReceiver";
    public static final String STORE_ACTION = "com.greenantmall.store.MallManagerReceiver";
    @Override
    public void onReceive(final Context context, Intent intent)
    {

        try
        {
            String action = intent.getAction();
            LogUtil.log.d(TAG, "Get Broadcat:" + action);
            if (STORE_ACTION.equals(action))
            {
                Bundle bundle = intent.getExtras();
                if (bundle != null)
                {
                    String channel = intent.getExtras().getString(
                        "com.avos.avoscloud.Channel");
                    // 获取消息内容
                    JSONObject json = new JSONObject(intent.getExtras()
                        .getString("com.avos.avoscloud.Data"));
                    iLog.d(TAG, "Get Broadcat json:" + json);
                    String msg = json.optString("msg");
                    long time = json.optLong("time");
                    String msgTypeId = json.optString("msgTypeId");
                    int msgType = json.optInt("msgType");
                    String userId = json.optString("userId");
                    String storeId = json.optString("storeId");

                    
                    String currentUserId = "";
                    AVUser user = AVUser.getCurrentUser();
                    if (user != null)
                    {
                        currentUserId = user.getObjectId();
                    }
                    
                    /**
                     * Get Broadcat json:{"action":"com.greenantmall.store.MallManagerReceiver",
                     * "time":1461662483480,"msgType":0,
                     * "storeId":"558e14bae4b060308e3d465e",
                     * "msgTypeId":"0921230205",
                     * "msg":"鎮ㄦ湁鏂扮殑璁㈠崟锛岃鍗曞彿涓猴細0921230205.",
                     * "userId":"55a3c118e4b0d6d690457775"}
                     */
                    // 为当前用户才进行通知
                 
                    if (!TextUtils.isEmpty(msg)
                            && (TextUtils.isEmpty(storeId) || storeId.equals(currentUserId))) {
                        notifyMsg(context, msg);
                        SQLiteHelper.getInstance(context).insertMsg(msg, time, msgTypeId, msgType,
                                storeId);
                    } else if (!TextUtils.isEmpty(msg)) {
                        SQLiteHelper.getInstance(context).insertMsg(msg, time, msgTypeId, msgType,
                                storeId);
                    }
                    iLog.d(TAG, "got action " + action + " on channel "
                        + channel + " with:" + msg+" currentUserId:"+currentUserId);
                }

            } 

        } catch (JSONException e)
        {
            Log.d(TAG, "JSONException: " + e.getMessage());
        }
    }

    private void notifyMsg(Context context, String msg)
    {
        NotificationManager nm = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification n = new Notification(R.drawable.ic_launcher, msg,
            System.currentTimeMillis());
        n.flags = Notification.FLAG_AUTO_CANCEL;
        Intent i = new Intent(context, MyMsgActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        // PendingIntent
        PendingIntent contentIntent = PendingIntent.getActivity(context,
            R.string.app_name, i, PendingIntent.FLAG_UPDATE_CURRENT);

        n.setLatestEventInfo(context,
            context.getString(R.string.order_msssage_title), msg,
            contentIntent);
        nm.notify(R.string.app_name, n);
    }

}
